package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DomainPackage.Havalimani;
import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;
public class SeferMapper implements IMapper {

	@Override
	public Object get(int ID) {
		Sefer sefer = new Sefer();
		ResultSet rs = DataConnector.getInstance().query("select * from sefer where seferid = " + ID + "");
		int h1ID = 0;
		int h2ID = 0;
		if (rs != null){
            
            try {
            		rs.next();
                    h1ID = Integer.parseInt(rs.getString(3));
                    h2ID = Integer.parseInt(rs.getString(4));
                    
                    sefer.setSeferID(Integer.parseInt(rs.getString(1)));
                    sefer.setSeferAdi(rs.getString(2));

                    sefer.setMesafe(Float.valueOf(rs.getString(5)));
                    sefer.setIrtifa(Float.valueOf(rs.getString(6)));
                    sefer.setUcusSuresi(Float.valueOf(rs.getString(7)));
            		
                    
            } catch (NumberFormatException e) {
                    System.out.println("SeferMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("SeferMapper: sql problem");
                    e.printStackTrace();
            }
    }
    else
            System.out.println("no sefer with given ID");
		
		HavalimaniMapper hm = new HavalimaniMapper();
        Havalimani h = (Havalimani) hm.get(h1ID);
		Havalimani h2 = (Havalimani) hm.get(h2ID);
		sefer.setKalkisHavalimani(h);
		sefer.setInisHavalimani(h2);
        
		return sefer;
	}

	@Override
	public int put(Object object) {
		Sefer sefer=(Sefer) object;
		

        if(!DataConnector.getInstance().update("INSERT INTO sefer "+
                "(seferid, seferadi, kalkishavalimani, inishavalimani, mesafe, irtifa, ucussuresi) " +
                "values ('"+sefer.getSeferID()+"', '"+
                sefer.getSeferAdi()+"', '"+
                sefer.getKalkisHavalimani().getHavalimaniID()+"', '"+
        		sefer.getInisHavalimani().getHavalimaniID()+"', '"+
                sefer.getMesafe()+"', '"+
                sefer.getIrtifa()+"', '"+
                sefer.getUcusSuresi()+"')")){
        	System.out.println("error");
        }
		return 0;
	}

	@Override
	public List<Object> getAll() {
		List<Object> allSeferList = new ArrayList<Object>();
    	ResultSet rs = DataConnector.getInstance().query("select * from sefer;");
    	int numberOfRows=0;
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
            numberOfRows = rsMetaData.getColumnCount()+1;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    	
    try {
    	if (rs != null){
            while(rs.next()){
                    
                    Sefer sefer= new Sefer();
                    	Havalimani h = new Havalimani();
                    	h.setHavalimaniID(Integer.parseInt(rs.getString(3)));
                    	Havalimani h2 = new Havalimani();
                    	h2.setHavalimaniID(Integer.parseInt(rs.getString(4)));
                    	sefer.setSeferID(Integer.parseInt(rs.getString(1)));
                        sefer.setSeferAdi(rs.getString(2));
                        sefer.setKalkisHavalimani(h);
                        sefer.setInisHavalimani(h2);
                        sefer.setMesafe(Float.valueOf(rs.getString(5)));
                        sefer.setIrtifa(Float.valueOf(rs.getString(6)));
                        sefer.setUcusSuresi(Float.valueOf(rs.getString(7)));
                            allSeferList.add(sefer);              
            }
    	}
    }
    catch(Exception e){
    	e.printStackTrace();
    }
    	for (Object se: allSeferList){
    		Sefer s = (Sefer) se;
    		HavalimaniMapper hm = new HavalimaniMapper();
    		Havalimani h1 = (Havalimani) hm.get(s.getKalkisHavalimani().getHavalimaniID());
    		Havalimani h2 = (Havalimani) hm.get(s.getInisHavalimani().getHavalimaniID());
    		s.setKalkisHavalimani(h1);
    		s.setInisHavalimani(h2);
    	}
    	return allSeferList;
	}

}
